// +----------------------------------------------------------------------
// | Think.Admin
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.ctolog.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zoujingli/Think.Admin
// +----------------------------------------------------------------------

define(['jquery', 'datetimepicker'], function($) {

    /*!
     * jQuery placeholder, fix for IE6,7,8,9
     */
    var JPlaceHolder = {
        _check: function() {
            return 'placeholder' in document.createElement('input');
        },
        init: function() {
            !this._check() && this.fix();
        },
        fix: function() {
            $(':input[placeholder]').map(function() {
                var self = $(this),
                    txt = self.attr('placeholder');
                self.wrap($('<div></div>').css({
                    zoom: '1',
                    margin: 'none',
                    border: 'none',
                    padding: 'none',
                    background: 'none',
                    position: 'relative'
                }));
                var pos = self.position(),
                    h = self.outerHeight(true),
                    paddingleft = self.css('padding-left');
                var holder = $('<span></span>').text(txt).css({
                    position: 'absolute',
                    left: pos.left,
                    top: pos.top,
                    height: h,
                    lineHeight: h + 'px',
                    paddingLeft: paddingleft,
                    color: '#aaa'
                }).appendTo(self.parent());
                self.on('focusin focusout change keyup', function() {
                    self.val() ? holder.hide() : holder.show();
                });
                holder.click(function() {
                    self.get(0).focus();
                });
                self.val() && holder.hide();
            });
        }
    };
    JPlaceHolder.init();

    /**
     * 定义消息处理构造方法
     */

    function msg() {
        this.version = '2.0';
        this.shade = [0.02, '#000'];
        this.closeIndexs = {};
    }

    /**
     * 关闭消息框
     */
    msg.prototype.close = function() {
        if (!this.closeIndexs['_' + this.index]) {
            this.closeIndexs['_' + this.index] = true;
            return layer.close(this.index);
        }
    };

    /**
     * 弹出警告消息框
     * @param msg 消息内容
     * @param callback 回调函数
     * @return {*|undefined}
     */
    msg.prototype.alert = function(msg, callback) {
        this.close();
        return this.index = layer.alert(msg, {
            end: callback,
            scrollbar: false
        });
    };

    /**
     * 确认对话框
     * @param msg 提示消息内容
     * @param ok 确认的回调函数
     * @param no 取消的回调函数
     * @return {undefined|*}
     */
    msg.prototype.confirm = function(msg, ok, no) {
        var self = this;
        return this.index = layer.confirm(msg, {
            btn: ['确认', '取消']
        }, function() {
            typeof ok === 'function' && ok.call(this);
            self.close();
        }, function() {
            typeof no === 'function' && no.call(this);
            self.close();
        });
    };

    /**
     * 显示成功类型的消息
     * @param msg 消息内容
     * @param time 延迟关闭时间
     * @param callback 回调函数
     * @return {common_L11._msg|*}
     */
    msg.prototype.success = function(msg, time, callback) {
        this.close();
        return this.index = layer.msg(msg, {
            icon: 1,
            shade: this.shade,
            scrollbar: false,
            end: callback,
            time: (time || 2) * 1000,
            shadeClose: true
        });
    };

    /**
     * 显示失败类型的消息
     * @param msg 消息内容
     * @param time 延迟关闭时间
     * @param callback 回调函数
     * @return {common_L11._msg|*}
     */
    msg.prototype.error = function(msg, time, callback) {
        this.close();
        return this.index = layer.msg(msg, {
            icon: 2,
            shade: this.shade,
            scrollbar: false,
            time: (time || 3) * 1000,
            end: callback,
            shadeClose: true
        });
    };

    /**
     * 状态消息提示
     * @param msg 消息内容
     * @param time 显示时间s
     * @param callback 回调函数
     * @return {common_L11._msg|*}
     */
    msg.prototype.tips = function(msg, time, callback) {
        this.close();
        return this.index = layer.msg(msg, {
            time: (time || 3) * 1000,
            shade: this.shade,
            end: callback,
            shadeClose: true
        });
    };

    /**
     * 显示正在加载中的提示
     * @param msg 提示内容
     * @param callback 回调方法
     * @return {common_L11._msg|*}
     */
    msg.prototype.loading = function(msg, callback) {
        this.close();
        return this.index = msg ? layer.msg(msg, {
            icon: 16,
            scrollbar: false,
            shade: this.shade,
            time: 0,
            end: callback
        }) : layer.load(2, {
            time: 0,
            scrollbar: false,
            shade: this.shade,
            end: callback
        });
    };

    /**
     * 自动处理显示Think返回的Json数据
     * @param data JSON数据对象
     * @param time 延迟关闭时间
     * @return {common_L11._msg|*}
     */
    msg.prototype.auto = function(data, time) {
        var self = this;
        if (parseInt(data.code) === 1) {
            return self.success(data.msg, time, function() { !! data.url ? (window.location.href = data.url) : $.form.reload();
                if (self.autoSuccessCloseIndexs && self.autoSuccessCloseIndexs.length > 0) {
                    for (var i in self.autoSuccessCloseIndexs) {
                        layer.close(self.autoSuccessCloseIndexs[i]);
                    }
                    self.autoSuccessCloseIndexs = [];
                }
            });
        }
        self.error(data.msg, 3, function() { !! data.url && (window.location.href = data.url);
        });
    };

    /**
     * auto处理成功的自动关闭
     * @param index
     */
    msg.prototype.addAutoSuccessCloseIndex = function(index) {
        this.autoSuccessCloseIndexs = this.autoSuccessCloseIndexs || [];
        this.autoSuccessCloseIndexs.push(index);
    };

    /**
     * 将消息对象挂载到Jq
     */
    $.msg = new msg();


    /**
     * 表单构造函数
     * @private
     */

    function _form() {
        this.version = '2.0';
        this.errMsg = '{status}服务器繁忙，请稍候再试！';
    }

    /**
     * 异步加载的数据
     * @param url 请求的地址
     * @param data 额外请求数据
     * @param type 提交的类型 GET|POST
     * @param callback 成功后的回调方法
     * @param loading 是否显示加载层
     * @param tips 提示消息
     * @param time 消息提示时间
     */
    _form.prototype.load = function(url, data, type, callback, loading, tips, time) {
        var self = this,
            dialogIndex = 0;
        (loading !== false) && (dialogIndex = $.msg.loading(tips));
        (typeof Pace === 'object') && Pace.restart();
        $.ajax({
            type: type || 'GET',
            url: $.menu.parseUri(url),
            data: data || {},
            statusCode: {
                404: function(re) {
                    if (window.APP_DUUG) {
                        layer.open({
                            title: self.errMsg.replace('{status}', 'E404 - '),
                            type: 2,
                            area: ['80%', '70%'],
                            maxmin: true,
                            content: url
                        });
                    } else {
                        self.show(re.responseText);
                        $.msg.tips(self.errMsg.replace('{status}', 'E404 - '));
                    }
                },
                500: function(re) {
                    if (window.APP_DUUG) {
                        layer.open({
                            title: self.errMsg.replace('{status}', 'E500 - '),
                            type: 2,
                            area: ['80%', '70%'],
                            maxmin: true,
                            content: url
                        });
                    } else {
                        self.show(re.responseText);
                        $.msg.tips(self.errMsg.replace('{status}', 'E500 - '));
                    }
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                $.msg.tips(self.errMsg.replace('{status}', 'E' + textStatus + ' - '));
            },
            success: function(res) {
                $.msg.close(dialogIndex);
                if (typeof callback === 'function' && callback.call(self, res) === false) {
                    return false;
                }
                if (typeof(res) === 'object') {
                    return $.msg.auto(res, time);
                }
                self.show(res);
            }
        });
    };

    /**
     * 动态HTML事件重载
     * @param $container
     */
    _form.prototype.reInit = function($container) {
        $.validate.listen.call(this), JPlaceHolder.init();
        window.form.render();
        $.topTable.listen();
        $container.find('[required]').parent().prevAll('label,th').addClass('label-required');
    };

    /**
     * 加载HTML到目标位置
     * @param url 目标URL地址
     * @param data URL参数
     * @param callback 回调函数
     * @param loading 是否显示加载
     * @param tips 提示消息
     */
    _form.prototype.open = function(url, data, callback, loading, tips) {
        this.load(url, data, 'GET', function(res) {
            if (typeof(res) === 'object') {
                return $.msg.auto(res);
            }
            var $container = $('.layer-main-container').html(res);
            reinit.call(this); //, setTimeout(reinit, 500), setTimeout(reinit, 1000);
            return (typeof callback === 'function') && callback.call(this);

            function reinit() {
                $.form.reInit($container);
            }
        }, loading, tips);
    };

    /**
     * 打开一个内置HTML页面
     * @param url
     * @param obj
     */
    _form.prototype.href = function(url, obj) {
        window.location.href = '#' + $.menu.parseUri(url, obj);
    };

    /**
     * 加载HTML到弹出层
     * @param url
     * @param data
     * @param title
     * @param area
     * @param btn
     * @param callback
     * @param loading
     * @param tips
     */
    _form.prototype.modal = function(url, data, title,area,btn,callback, loading, tips) {
        var full = false;

        this.load(url, data, 'GET', function(res) {
            if (typeof(res) === 'object') {
                return $.msg.auto(res);
            }
            full = area == 'full' || false;
            area = ( area != 'full' ? area : '500px');
            btn = btn ? btn.split('|') : false;

            var index = layer.open({
                type: 1,
                btn: btn || ['保存数据','取消编辑'],
                maxmin: true,
                area: area || "500px",
                content: res,
                title: title || '',
                success: function(dom, index) {
                    // 此窗口完成时需要自动关闭
                    $.msg.addAutoSuccessCloseIndex(index);
                    var $container = $(dom); /* 处理样式及返回按钮事件 */
                    $container.find('[data-close]').off('click').on('click', function() {
                        if ($(this).attr('data-confirm')) {
                            $.msg.confirm($(this).attr('data-confirm'), function() {
                                layer.close(index);
                            });
                        } else {
                            layer.close(index);
                        }
                    });
                    /* 事件重载 */
                    $.form.reInit($container);
                },yes:function(index, dom){
                    $(dom).find('form').submit();
                },btn2:function (index, dom){
                    var $confirm = $(dom).find('form').data('confirm');
                    if (  $confirm ) {
                        $.msg.confirm( $confirm , function() {
                            layer.close(index);
                        });
                    } else {
                        layer.close(index);
                    }
                    return false;
                }
            });
            if(full)  layer.full(index);
            return (typeof callback === 'function') && callback.call(this);


        }, loading, tips);
    };

    /**
     * 显示HTML到中主内容区
     * @param html
     */
    _form.prototype.show = function(html) {
        var $container = $('.layer-main-container').html(html);
        reinit.call(this); //, setTimeout(reinit, 500), setTimeout(reinit, 1000);
        function reinit() {
            $.form.reInit($container);
        }
    };

    /**
     * 打开一个iframe窗口
     * @param url iframe打开的URL地址
     * @param title 窗口标题
     */
    _form.prototype.iframe = function(url, title) {
        return layer.open({
            title: title || '窗口',
            type: 2,
            area: ['800px', '530px'],
            fix: true,
            maxmin: false,
            content: url
        });
    };

    /**
     * 关闭FORM框
     * @return {*|jQuery}
     */
    _form.prototype.close = function() {
        return $(this._modal).modal('hide');
    };

    /**
     * 刷新当前页面
     */
    _form.prototype.reload = function() {
        window.onhashchange.call(this);
    };

    /*!表单实例挂载*/
    $.form = new _form();

    /**
     * 定义模块函数
     * @returns {validate_L1.validate}
     */
    var validate = function() {
        // 表单元素
        this.inputTag = 'input,textarea,select';
        // 检测元素事件
        this.checkEvent = {
            change: true,
            blur: true,
            keyup: false
        };
    };

    /**
     *获取表单元素的类型
     */
    validate.prototype.getElementType = function(ele) {
        return (ele.getAttribute("type") + "").replace(/\W+$/, "").toLowerCase();
    };

    /**
     *去除字符串两头的空格
     */
    validate.prototype.trim = function(str) {
        return str.replace(/(^\s*)|(\s*$)/g, '');
    };


    /**
     * 标签元素是否可见
     * @returns {Boolean}
     */
    validate.prototype.isVisible = function(ele) {
        return $(ele).is(':visible');
    };

    /**
     * 检测属性是否有定义
     * @param {type} ele
     * @param {type} prop
     * @param {type} undefined
     * @returns {Boolean}
     */
    validate.prototype.hasProp = function(ele, prop, undefined) {
        if (typeof prop !== "string") {
            return false;
        }
        var attrProp = ele.getAttribute(prop);
        return (attrProp !== undefined && attrProp !== null && attrProp !== false)
    };

    /**
     * 判断表单元素是否为空
     * @param {type} ele
     * @param {type} value
     * @returns {Boolean}
     */
    validate.prototype.isEmpty = function(ele, value) {
        value = value || ele.getAttribute('placeholder');
        var trimValue = this.trim(ele.value);
        return (trimValue === "" || trimValue === value);
    };

    /**
     * 正则验证表单元素
     * @param {type} ele
     * @param {type} regex
     * @param {type} params
     * @returns {Boolean}
     */
    validate.prototype.isRegex = function(ele, regex, params) {
        // 原始值和处理值
        var inputValue = ele.value,
            dealValue = inputValue;
        var self = this,
            type = this.getElementType(ele);
        if (type !== "password") { // 密码不trim前后空格
            dealValue = this.trim(inputValue);
            if (dealValue !== inputValue) {
                if (ele.tagName.toLowerCase() !== "textarea") {
                    ele.value = dealValue;
                } else {
                    ele.innerHTML = dealValue;
                }
            }
        }
        // 获取正则表达式，pattern属性获取优先，然后通过type类型匹配。注意，不处理为空的情况
        regex = regex || ele.getAttribute('pattern');
        if (dealValue === "" || !regex) {
            return true;
        }
        // multiple多数据的处理
        var isMultiple = this.hasProp(ele, 'multiple'),
            newRegExp = new RegExp(regex, params || 'i');
        // number类型下multiple是无效的
        if (isMultiple && !/^number|range$/i.test(type)) {
            var isAllPass = true;
            var dealValues = dealValue.split(",");
            for (var i in dealValues) {
                var partValue = self.trim(dealValues[i]);
                if (isAllPass && !newRegExp.test(partValue)) {
                    isAllPass = false;
                }
            }
            return isAllPass;
        } else {
            return newRegExp.test(dealValue);
        }
    };

    /**
     * 检侧所的表单元素
     */
    validate.prototype.isAllpass = function(elements, options) {
        if (!elements) {
            return true;
        }
        var allpass = true,
            self = this,
            params = options || {};

        if (elements.size && elements.size() === 1 && elements.get(0).tagName.toLowerCase() === "form") {
            elements = $(elements).find(self.inputTag);
        } else if (elements.tagName && elements.tagName.toLowerCase() === "form") {
            elements = $(elements).find(self.inputTag);
        }
        elements.each(function() {
            if (self.checkInput(this, params) === false) {
                return $(this).focus(), (allpass = false);
            }
        });
        return allpass;
    };

    /**
     * 验证标志
     */
    validate.prototype.remind = function(input, type, tag) {
        var text = '';
        // 如果元素完全显示
        if (this.isVisible(input)) {
            if (type === "radio" || type === "checkbox") {
                this.errorPlacement(input, this.getErrMsg(input));
            } else if (tag === "select" || tag === "empty") {
                // 下拉值为空或文本框文本域等为空
                this.errorPlacement(input, (tag === "empty" && text) ? "您尚未输入" + text : this.getErrMsg(input));
            } else if (/^range|number$/i.test(type) && Number(input.value)) {
                // 整数值与数值的特殊提示
                this.errorPlacement(input, "值无效");
            } else {
                // 文本框文本域格式不准确
                var finalText = this.getErrMsg(input);
                if (text) {
                    finalText = "您输入的" + text + "格式不准确";
                }
                this.errorPlacement(input, finalText);
            }
        }
        return false;
    };

    /**
     * 检测表单单元
     */
    validate.prototype.checkInput = function(input, options) {
        var type = this.getElementType(input);
        var tag = input.tagName.toLowerCase();
        var isRequired = this.hasProp(input, "required");
        var isNone = this.hasProp(input, 'data-auto-none');
        //无需要验证
        if (isNone || input.disabled || type === 'submit' || type === 'reset' || type === 'file' || type === 'image' || !this.isVisible(input)) {
            return;
        }

        var allpass = true;
        // 需要验证的有
        if (type === "radio" && isRequired) {
            var eleRadios = input.name ? $("input[type='radio'][name='" + input.name + "']") : $(input);
            var radiopass = false;
            eleRadios.each(function() {
                if (radiopass === false && $(this).is("[checked]")) {
                    radiopass = true;
                }
            });
            if (radiopass === false) {
                allpass = this.remind(eleRadios.get(0), type, tag);
            } else {
                this.successPlacement(input);
            }
        } else if (type === "checkbox" && isRequired && !$(input).is("[checked]")) {
            allpass = this.remind(input, type, tag);
        } else if (tag === "select" && isRequired && !input.value) {
            allpass = this.remind(input, type, tag);
        } else if ((isRequired && this.isEmpty(input)) || !(allpass = this.isRegex(input))) {
            allpass ? this.remind(input, type, "empty") : this.remind(input, type, tag);
            allpass = false;
        } else {
            this.successPlacement(input);
        }
        return allpass;
    };

    /**
     *获取错误提示的内容
     */
    validate.prototype.getErrMsg = function(ele) {
        return ele.getAttribute('title') || '';
    };

    /**
     * 错误消息显示
     * @param {type} ele
     * @param {type} content
     * @param {type} options
     * @returns {undefined}
     */
    validate.prototype.errorPlacement = function(ele, content) {
        $(ele).addClass('validate-error').addClass('layui-form-danger'), this.insertErrorEle(ele);
        $($(ele).data('input-info')).addClass('fadeInRight animated').css({
            width: 'auto'
        }).html(content);
    };

    /**
     * 错误消息消除
     */
    validate.prototype.successPlacement = function(ele) {
        $(ele).removeClass('validate-error').removeClass('layui-form-danger'), this.insertErrorEle(ele);
        $($(ele).data('input-info')).removeClass('fadeInRight').css({
            width: '30px'
        }).html('');
    };

    /**
     * 错误消息标签插入
     * @param {type} ele
     * @returns {undefined}
     */
    validate.prototype.insertErrorEle = function(ele) {
        var $html = $('<span style="-webkit-animation-duration:.2s;animation-duration:.2s;padding-right:20px;color:#a94442;position:absolute;right:0;font-size:12px;z-index:2;display:block;width:34px;text-align:center;pointer-events:none"></span>');
        $html.css({
            top: $(ele).position().top + 'px',
            paddingTop: $(ele).css('paddingTop'),
            paddingBottom: $(ele).css('paddingBottom'),
            lineHeight: $(ele).css('lineHeight')
        });
        $(ele).data('input-info') || $(ele).data('input-info', $html.insertAfter(ele));
    };

    /**
     * 表单验证入口
     * @param {type} callback
     * @param {type} options
     * @returns {$|Function|Zepto}
     */
    validate.prototype.check = function(form, callback, options) {
        var params = $.extend({}, options || {}), self = this;
        // 去除HTML默认验证
        $(form).attr("novalidate", "novalidate");

        // 表单元素动态监听
        $(form).find(self.inputTag).map(function() {
            var func = function() {
                self.checkInput(this);
            };
            for (var i in self.checkEvent) {
                if (self.checkEvent[i] === true) {
                    $(this).off(i, func).on(i, func);
                }
            }
        });
        // 表单提交事情监听
        $(form).bind("submit", function(event) {
            if (self.isAllpass($(this).find(self.inputTag), params) && typeof callback === 'function') {
                callback.call(this, $(form).serialize());
            }
            return event.preventDefault(), false;
        });
        // 表单对象绑定
        return $(form).data('validate', this);
    };

    /**
     * 注册对象到JqFn
     * @param callback
     * @param options
     * @return {$|Function|Zepto}
     */
    $.fn.validate = function(callback, options) {
        return (new validate()).check(this, callback, options);
    };

    /**
     * 注册对象到Jq
     * @param form
     * @param callback
     * @param options
     * @return {$|Function|Zepto}
     */
    $.validate = function(form, callback, options) {
        return (new validate()).check(form, callback, options);
    };

    /*! 自动监听规则内表单 */
    $.validate.listen = function() {
        $('form[data-auto]').map(function() {
            if ($(this).attr('data-listen') !== 'true') {
                // 表单监听初始化
                var callback = $(this).attr('data-callback');
                $(this).attr('data-listen', 'true').validate(function(data) {
                    $.form.load(this.getAttribute('action') || window.location.href, data, this.getAttribute('method') || 'POST', window[callback || '_default_callback'] || undefined, true, this.getAttribute('data-tips') || undefined, this.getAttribute('data-time') || undefined);
                });
                // 表单监听初始完成后的处理
                $(this).find('[data-form-loaded]').map(function() {
                    $(this).html(this.getAttribute('data-form-loaded') || this.innerHTML);
                    $(this).removeAttr('data-form-loaded').removeClass('layui-disabled');
                });
            }
        });
    };

    /**
     * 后台菜单辅助插件
     * @returns {plugsL#14.menu}
     */
    var menu = function() {
        this.version = '1.0';
    };

    /*! 计算URL地址中有效的URI */
    menu.prototype.getUri = function(uri) {
        uri = uri || window.location.href;
        uri = (uri.indexOf(window.location.host) !== -1 ? uri.split(window.location.host)[1] : uri).split('?')[0];
        return (uri.indexOf('#') !== -1 ? uri.split('#')[1] : uri);
    };

    /*! 通过URI查询最有可能的菜单NODE */
    menu.prototype.queryNode = function(url) {
        var $menu = $('[data-menu-node][data-open*="_URL_"]'.replace('_URL_', url.replace(/\.html$/ig, '')));
        if ($menu.size()) {
            return $menu.get(0).getAttribute('data-menu-node');
        }
        return /^m\-/.test(node = location.href.replace(/.*spm=([\d\-m]+).*/ig, '$1')) ? node : '';
    };

    /*!　URL转URI */
    menu.prototype.parseUri = function(uri, obj) {
        var params = {};
        if (uri.indexOf('?') !== -1) {
            var queryParams = uri.split('?')[1].split('&');
            for (var i in queryParams) {
                if (queryParams[i].indexOf('=') !== -1) {
                    var hash = queryParams[i].split('=');
                    try {
                        params[hash[0]] = window.decodeURIComponent(window.decodeURIComponent(hash[1].replace(/%2B/ig, ' ')));
                    } catch (e) {
                        console.log([e, uri, queryParams, hash]);
                    }
                }
            }
        }
        uri = this.getUri(uri);
        params.spm = obj && obj.getAttribute('data-menu-node') || this.queryNode(uri);
        if (!params.token) {
            var token = window.location.href.replace(/.*token=(\w+).*/ig, '$1');
            (/^\w{16}$/.test(token)) && (params.token = token);
        }
        delete params[""];
        var query = '?' + $.param(params);
        return uri + (query !== '?' ? query : '');
    };
    menu.prototype.getUrlParam = function(name, uri) {
        uri = uri || window.location.hash;
        var reg = new RegExp("(^|&?)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = uri.substr(1).match(reg); //匹配目标参数
        if (r != null) return unescape(r[2]);
        return null; //返回参数值
    };


    /*! 后台菜单动作初始化 */
    menu.prototype.listen = function() { /*! 左侧菜单状态切换 */
        //            $(this).parents('div[data-menu-box] > div').removeClass('open').find('ul.sidebar-trans').slideUp();

        $('ul.sidebar-trans .nav-item a').on('click', function() {
            $(this).parents('.sidebar-nav.main-nav').addClass('open').find('ul.sidebar-trans').slideDown();
            $('.sidebar-trans .nav-item').not($(this).parent().addClass('active')).removeClass('active');
        });


        $('body').on('click', '.framework-sidebar-full .sidebar-title', function() {
            $(this).parent().siblings('.sidebar-nav').removeClass('open').find('ul.sidebar-trans').slideUp();
            var $trans = $(this).next('ul.sidebar-trans'),
                node = $trans.attr('data-menu-node') || false;
            node && $.cookie(node, $(this).parent().toggleClass('open').hasClass('open') ? 2 : 1);
            $(this).parent().hasClass('open') ? $trans.slideDown() : $trans.slideUp();
        });


        //$('ul.sidebar-trans').map(function() {
        //     var node = this.getAttribute('data-menu-node') || false;
        //    node && (parseInt($.cookie(node) || 2) === 2) && $(this).slideDown().parent().addClass('open');
        //});

        /*! Mini 菜单模式 Tips 显示*/
        $('body').on('mouseenter mouseleave', '.framework-sidebar-mini .sidebar-trans .nav-item a,.framework-sidebar-mini .sidebar-title div', function(e) {
            $(this).tooltip({
                template: '<div class="console-sidebar-tooltip" role="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>',
                title: $(this).text(),
                placement: 'right',
                html:false,
                container: 'body'
            }).tooltip('show'), (e.type === 'mouseleave') && $(this).tooltip('destroy');
        });

        /*! 切换左侧菜单 */
        var $menutarget = $('[data-menu-target]').on('click', function() {
            $menutarget.not($(this).addClass('active')).removeClass('active');
            var menuNode = $(this).attr('data-menu-target');
            var $leftmenu = $('[data-menu-box=' + menuNode + ']').removeClass('hide');
            $("[data-menu-box]").not($leftmenu).addClass('hide');
            var tab = $leftmenu.find('[data-open]:first').trigger('click');
        });

        $('[data-open]').on('click',function(){
            var tM = $(this).parent('li.nav-item').find('[nc-menu-temp]'),TmH = tM.html(),Ntb = $('.nv-layui-tab'),container = $('.layer-main-container');
            Ntb.empty();container.css('top','0');
           if(tM.length > 0){
               container.css('top','40px');
               Ntb.html(TmH).find('[data-open]:first').addClass('layui-this').trigger('click');
               return false
           }
        });

        //

        /*! 左侧菜单样式切换 */
        var $targetmenu = $('.sidebar-fold').on('click', function() {
            var $body = $('.framework-body').toggleClass('framework-sidebar-mini framework-sidebar-full');
            $.cookie('menu-style', $body.hasClass('framework-sidebar-mini') ? 'mini' : 'full');
        });

        ($.cookie('menu-style') !== 'mini') && $targetmenu.trigger('click');



        /*! URI路由处理 */
        window.onhashchange = function() {
            var hash = (window.location.hash || '').substring(1),
                node = hash.replace(/.*spm=([\d\-m]+).*/ig, "$1");
            if (!/^m\-[\d\-]+$/.test(node)) {
                node = $.menu.queryNode($.menu.getUri()) || '';
            }

            //alert(node.length)

            if (hash.length < 1 || node.length < 1) {
                return $('.topbar-home-link:first').trigger('click');
            } /* 顶部菜单选中处理 */
            var parentNode = [node.split('-')[0], node.split('-')[1]].join('-');

            $('[data-menu-target]').not($('[data-menu-target="' + parentNode + '"]').addClass('active')).removeClass('active'); /* 左则菜单处理 */
            var $menu = $('[data-menu-node="' + node + '"]').eq(0);

            if ($menu.size() > 0) {
                $('.framework-container').addClass('framework-sidebar-full').css('top','0');
                var $li = $menu.parent('li').addClass('active');
                $li.parents('.framework-sidebar').find('li.active').not($li).removeClass('active');
                $menu.parents('.sidebar-trans').removeClass('hide').show();
                $menu.parents('.main-nav').addClass('open');
                $menu.parents('[data-menu-box]').removeClass('hide').siblings('[data-menu-box]').addClass('hide');
                if($li.find('[nc-menu-temp]').length > 0){
                    var TmH = $li.find('[nc-menu-temp]').html(),Ntb = $('.nv-layui-tab'),container = $('.layer-main-container');
                    Ntb.empty();
                    container.css('top','40px');
                    Ntb.html(TmH).find('li[data-open]').each(function(){
                        if( $(this).data('open') == $.menu.getUri()){
                            $(this).addClass('layui-this');
                        }
                    })
                }
            } else {
                $('.framework-container').removeClass('framework-sidebar-full');
            }

            $.form.open(hash);
        };
        // URI初始化动作
        window.onhashchange.call(this);
    };

    /*! 实例并加载到jQ对象上 */
    $.menu = new menu();


    /*! 头部工具功能 */
    var topTool = function() {
        this.version = '1.0';
    };
    topTool.prototype.init = function() {
        $('[data-reload]').hover(function() {
            $(this).find('.glyphicon').addClass('fa-spin');
        }, function() {
            $(this).find('.glyphicon').removeClass('fa-spin');
        });
    };
    topTool.prototype.entryFullScreen = function() {
        var docE = document.documentElement;
        if (docE.requestFullScreen) {
            docE.requestFullScreen();
        } else if (docE.mozRequestFullScreen) {
            docE.mozRequestFullScreen();
        } else if (docE.webkitRequestFullScreen) {
            docE.webkitRequestFullScreen();
        }
    };
    topTool.prototype.exitFullScreen = function() {
        var docE = document;
        if (docE.exitFullscreen) {
            docE.exitFullscreen();
        } else if (docE.mozCancelFullScreen) {
            docE.mozCancelFullScreen();
        } else if (docE.webkitCancelFullScreen) {
            docE.webkitCancelFullScreen();
        }
    };

    topTool.prototype.panel = function() {
        var el = $(this).parents(".panel").children(".panel-body");
        if ($(this).hasClass("iconpx-chevron-down")) {
            $(this).removeClass("iconpx-chevron-down").addClass("iconpx-chevron-up");
            el.slideUp(200);
        } else {
            $(this).removeClass("iconpx-chevron-up").addClass("iconpx-chevron-down");
            el.slideDown(200);
        }
    };
    /**
     * 锁屏事件
     * @param status 0 未锁屏，1 锁屏
     * @constructor
     */
    topTool.prototype.LockScreen = function(locked) {
        var startTimer = function() {
            var today = new Date();
            var h = today.getHours();
            var m = today.getMinutes();
            var s = today.getSeconds();
            m = m < 10 ? '0' + m : m;
            s = s < 10 ? '0' + s : s;
            $('#time').html(h + ":" + m + ":" + s);
            t = setTimeout(function() {
                startTimer()
            }, 500)
        };
        if (locked == 1) {
            $('.lock-screen').show();
            $('#locker').show();
            $('#layui_layout').hide();
            $('#lock_password').val('');
            startTimer();
        } else {
            $('.lock-screen').hide();
            $('#locker').hide();
            $('#layui_layout').show()
        }
    };
    $.topTool = new topTool();

    /*! 聊天工具 */
    var layim = function() {
        this.version = '1.0';
    };

    layim.prototype.init = function() {
        window.layim.config({
            //初始化接口
            init: {
                url: '/layim/json/getList.json',
                data: {}
            }
            //查看群员接口
            ,
            members: {
                url: '/layim/json/getMembers.json',
                data: {}
            }

            ,
            uploadImage: {
                url: '' //（返回的数据格式见下文）
                ,
                type: '' //默认post
            },
            uploadFile: {
                url: '' //（返回的数据格式见下文）
                ,
                type: '' //默认post
            }

            ,
            isAudio: true //开启聊天工具栏音频
            ,
            isVideo: true //开启聊天工具栏视频

            //扩展工具栏
            ,
            tool: [{
                alias: 'code',
                title: '代码',
                icon: '&#xe64e;'
            }]

            //,brief: true //是否简约模式（若开启则不显示主面板）

            //,title: 'WebIM' //自定义主面板最小化时的标题
            //,right: '100px' //主面板相对浏览器右侧距离
            //,minRight: '90px' //聊天面板最小化时相对浏览器右侧距离
            ,
            initSkin: '3.jpg' //1-5 设置初始背景
            //,skin: ['aaa.jpg'] //新增皮肤
            //,isfriend: false //是否开启好友
            //,isgroup: false //是否开启群组
            //,min: true //是否始终最小化主面板，默认false
            //,notice: true //是否开启桌面消息提醒，默认false
            //,voice: false //声音提醒，默认开启，声音文件为：default.mp3

            ,
            msgbox: '/layim/demo/msgbox.html' //消息盒子页面地址，若不开启，剔除该项即可
            ,
            find: '/layim/demo/find.html' //发现页面地址，若不开启，剔除该项即可
            ,
            chatLog: '/layim/demo/chatLog.html' //聊天记录页面地址，若不开启，剔除该项即可

        });
    }

    /*! 表格工具  */
    var topTable = function() {
        this.version = '1.0';
        this.url = '';
    };
    /*!
     表格点击修改
     */
    topTable.prototype.tdedit = function() {
        var s_val = $(this).text(),
            s_name = $(this).attr('data-field'),
            s_id = $(this).attr('data-id'),
            acturi = $(this).parents('.J_tablelist').data('acturi');
        $('<input type="text" class="layui-table-edit"  value="' + s_val + '" />').focusout(function() {
            $(this).prev('span').show().text($(this).val());
            if ($(this).val() != s_val) {
                $.form.load(acturi, {
                    id: s_id,
                    field: s_name,
                    val: $(this).val()
                }, 'post', function(re) {
                    if (re.code == 0) {
                        $('span[data-field="' + s_name + '"][data-id="' + s_id + '"]').text(s_val);
                    }
                    return false;
                });
            }
            $(this).remove();
        }).insertAfter($(this)).focus().select();
        $(this).hide();
        return false;
    };
    /*!
     表格排序
     */
    topTable.prototype.order_by = {
        order: '',
        click: function() {
            var sort = $(this).data('field'),
                local_url = $.menu.getUri(),
                sort_url = (local_url.indexOf('?') < 0) ? '?' : '&';
            var order = (topTable.prototype.order_by.order == 'asc' ? 'desc' : 'asc');
            sort_url += 'sort=' + sort + '&order=' + order;
            $.form.open(local_url + sort_url, {}, function() {
                setTimeout(function() {
                    $('span[data-tdtype="order_by"]').each(function() {
                        if ($(this).data('field') == sort) {
                            $(this).addClass("sort_" + order);
                            topTable.prototype.order_by.order = order;
                        }
                    });
                }, 100)
            });
        }
    };
    /*!
     表格排序
     */
    topTable.prototype.
        switch = function() {
        var _this = $(this),
            text = $(this).data('text'),
            style = $(this).data('style'),
            s_name = $(this).data('field'),
            auth = $(this).data('auth'),
            s_id = $(this).data('id'),
            value = ($(this).data('value')) == 0 ? 1 : 0,
            acturi = $(this).parents('.J_tablelist').data('acturi');
        style = style || '|normal';
        style = style.split('|');
        text = text || '否|是';
        text = text.split('|');
        $.form.load(acturi, {
            id: s_id,
            field: s_name,
            val: value
        }, 'post', function(re) {
            if (re.code == 1) {
                $.each(style, function(a, b) {
                    _this.removeClass('layui-btn-' + b);
                });
                _this.addClass('layui-btn-' + style[value]).data('value', value).text(text[value]);
            }
            return false;
        });
    };
    topTable.prototype.upimg = function() {
        var _this = $(this),
            value = _this.data('value'),
            name = _this.data('name'),
            required = _this.required || false,
            title = _this.attr('title') || '',
            width = _this.data('w') || 0,
            height = _this.data('h') || 0,
            exts = _this.data('ext') || 'jpg|png|gif|bmp|jpeg',
            token = _this.data('token') || '',
            desc = _this.data('desc') || false,
            num = _this.data('num') || 'on',
            action = _this.data('action') || '/admin/index/upload.html',
            inwidth = width!=0 ? 'style="width:'+width+'"' : '';
         var temp = '<span class="layui-inline text-center"><i class="fa fa-image"></i></span>' + '<div class="layui-inline" '+inwidth+'><input type="text"  title="'+title+'" class="layui-input" '+ ( required == false ? 'required="required"' : '') +' name="' + name + '"></div>' + '<button update type="button" class="layui-btn">上传图片</button>' + '<button reload type="button" class="layui-btn layui-btn-danger layui-hide">重传</button>' + (desc ? '<p class="help-block">' + desc + '</p>' : '');
         var model = _this.html(temp),
            input = model.find('input');

        if (typeof value != 'undefined' && value != '') {
            model.find('span').empty().html('<img data-tips-image src="' + value + '"/>');
            model.find('div>input').val(value);
        }
        input.change(function() {
            model.find('img').attr('src', this.value)
        });
        var uploadInst = window.upload.render({
            elem: model.find('button[update]'),
            data: {
                token: token
            },
            exts: exts,
            url: action,
            before: function(obj) {
                model.find('button[reload]').addClass('layui-hide');
                obj.preview(function(index, file, result) {
                    model.find('span').empty().html('<img data-tips-image src="' + value + '"/>').attr('src', result);
                });
            },
            done: function(res) {
                if (res.code == 0) {
                    return layer.msg((typeof res.msg == 'undefined' || res.msg == '' ? '上传失败' : res.msg));
                } else {
                    model.find('img').attr('src', res.data); // 链接地址
                    model.find('div>input').val(res.data);
                }
            },
            error: function() {
                //演示失败状态，并实现重传
                model.find('button[reload]').removeClass('layui-hide').off('click').on('click', function() {
                    uploadInst.upload();
                });
            }
        });
    };
    topTable.prototype.listen = function() {
        //topTable.prototype.order_by.init();
        $('.J_tablelist').off('click').on('click', '[data-tdtype=order_by]', topTable.prototype.order_by.click).
        on('click', '[data-tdtype=edit]', topTable.prototype.tdedit).
        on('click', '[data-tdtype=switch]', topTable.prototype.
            switch);

        //监听加载 IMG 上传
        $('[data-tdtype=upimg]').each(topTable.prototype.upimg);

        //监听事件 控件渲染
        $('[data-tdtype=date]').each(function() {
            var _this = this,
                data = $(_this).data('data'),
                format = $(_this).data('format') || 'Y-m-d',
                timepicker = false;
            if (format.indexOf('H') > -1 || format.indexOf('i') > -1 || format.indexOf('s') > -1 || format.indexOf(':') > -1) {
                timepicker = true;
            }
            $(_this).datetimepicker({
                lang: 'ch',
                format: format,
                timepicker: timepicker,
            });
        });



    };

    $.topTable = new topTable();
});